$(function() {
    $( "#tabs" ).tabs();

    window.jsonParser = new JsonParser("");

    $("#secretKey").val($("option:selected", $("#selClient")).attr("key"));
    $("#clientId").val($("option:selected", $("#selClient")).attr("cid"));

    $("#selClient").change(function(){
        var optionSelected = $("option:selected", this);
        $("#secretKey").val($(optionSelected).attr("key"));
        $("#clientId").val($(optionSelected).attr("cid"));

        $("#p_clientId").val( $("#selClient").val());
        $("#p_secretKey").val( $("#secretKey").val());
    })

    $("#p_clientId").val( $("#selClient").val());
    $("#p_secretKey").val( $("#secretKey").val());
});

function showResult(settings){
    $.fn.mask();
    $("#requestedUrl").val(  $("#requestedUrl").attr("domain") + settings.url);
    $("#p_data").text(settings.data == null?"":settings.data );

    var timestamp = Math.round(new Date().getTime());
    var ascription = $("#ascription").val();

    var sign = [];
    sign.push("uri=" + settings.url);
    sign.push("&clientId=" + $("#clientId").val());
    sign.push("&method=" + settings.type);
    sign.push("&ascription=" + ascription);
    sign.push("&timestamp=" + timestamp);

    console.log("签名加密前：" + sign.join(''));

    var signatureStr = CryptoJS.HmacSHA1(sign.join(''), $("#secretKey").val()).toString(CryptoJS.enc.Base64);

    console.log("签名加密后：" + signatureStr);

    ascription = encodeURI(ascription);

    $.ajax({
        type: settings.type,
        url:settings.url,
        dataType: settings.dataType,
        data: settings.data,
        contentType: "application/json",
        headers:{
            "Client-Id": $("#clientId").val(),
            "Client-Signature": signatureStr,
            "Ascription": ascription,
            "Timestamp": timestamp,
            "Secret-Key" : $("#secretKey").val(),
            "Visitor-Id" : $("#visitorId").val(),
            "Session-Id" : $("#sessionId").val()
        },
        success:function(data){

            window.jsonParser.jsonContent = JSON.stringify(data);;
            window.jsonParser.init();

            if(settings.success)
                settings.success(data);

            $.fn.mask.close();
        },
        error: function(data){
            window.jsonParser.jsonContent = JSON.stringify(data);;
            window.jsonParser.init();
            $.fn.mask.close();
        }
    });
}

var oa = {
    getMember : function(){
        var settings = {
            type:"GET",
            url:"/uj-oa-api/member/" + $("#memberId").val(),
            dataType: "json"
        };
        showResult(settings);
    }
}
